package com.markerhub.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.markerhub.application.service.QueryPersonInfoService;
import com.markerhub.common.enums.SmsTypeEnums;
import com.markerhub.common.lang.Result;
import com.markerhub.entity.TUnusualPerson;
import com.markerhub.service.ITUnusualPersonService;
import com.markerhub.vo.BasePage;
import com.markerhub.vo.response.PersonInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
* 异常人员表前端控制器
* @author zhz
* @since 2021-9-18 11:38:20
*/

@Slf4j
@Api(tags = "异常人员表", description = "异常人员表相关API")
@RestController
@RequestMapping("/unusual/person")
public class TUnusualPersonController extends BaseController {

    @Autowired
    private ITUnusualPersonService tUnusualPersonService;

    @Autowired
    private QueryPersonInfoService queryPersonInfoService;

    /**
     * 新增
     */
    @ApiOperation(value = "新增", notes = "新增")
    @PostMapping("/save")
    public Result save(@RequestBody TUnusualPerson tUnusualPerson){
        tUnusualPersonService.save(tUnusualPerson);
        return Result.succ("");
    }

    /**
     * 根据id删除
     */
    @ApiOperation(value = "根据id删除", notes = "根据id删除")
    @PostMapping("/delete")
    public Result delete(@RequestBody Long[] ids){
        tUnusualPersonService.removeByIds(Arrays.asList(ids));
        return Result.succ("");
    }

    /**
     * 条件查询
     */
    @ApiOperation(value = "条件查询", notes = "条件查询")
    @PostMapping("/get")
    public Result list(@RequestBody TUnusualPerson tUnusualPerson){
        List<TUnusualPerson> tUnusualPersonList = tUnusualPersonService.list(new QueryWrapper<>(tUnusualPerson));
        return Result.succ(tUnusualPersonList);
    }

    /**
     * 列表(分页)
     */
    @ApiOperation(value = "列表(分页)", notes = "列表(分页)")
    @PostMapping("/list")
    public Result pageList(@RequestBody BasePage basePage){
        Page<TUnusualPerson> page = new Page<>(basePage.getCurrent(),basePage.getSize());
        Page<TUnusualPerson> dataPage = tUnusualPersonService.page(page, null);
        dataPage.getRecords().forEach(item ->{
            item.setSmsType(SmsTypeEnums.getNameByCode(item.getSmsType()));
        });
        return Result.succ(page);
    }

    /**
     * 根据id查询
     */
    @ApiOperation(value = "根据id查询", notes = "根据id查询")
    @GetMapping("/info/{id}")
    public Result get(@PathVariable("id") String id){
        TUnusualPerson tUnusualPerson = tUnusualPersonService.getById(id);
        return Result.succ(tUnusualPerson);
    }

    /**
     * 根据id修改
     */
    @ApiOperation(value = "根据id修改", notes = "根据id修改")
    @PostMapping("/update")
    public Result update(@RequestBody TUnusualPerson tUnusualPerson){
        tUnusualPersonService.updateById(tUnusualPerson);
        return Result.succ("");
    }

    /**
     * 获取健康码信息
     */
    @GetMapping("/healthCodeInfo/{size}")
    public Result queryHealthCodeInfo(@PathVariable("size") int size){
        List<PersonInfoVO> list = queryPersonInfoService.queryPersonInfo(size);
        return Result.succ(list);
    }

}
